package org.theSeed.springSingleDataSource.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.theSeed.springDatasouceProp.pojo.SeedBaseDataSourceHolder;

import javax.sql.DataSource;
import java.util.Map;

/**
 * 单数据源配置
 */
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "seed-data-source-prop",name = "seed-spring-data-source-type",havingValue = "single")
@AutoConfigureBefore({DataSourceAutoConfiguration.class})
public class SingleDataSourceConfig {
    @Autowired
    private SeedBaseDataSourceHolder seedBaseDataSourceHolder;

    /**
     * 单数据源创建
     * @return
     */
    @Bean
    @Primary
    public DataSource dataSource(){
        Map.Entry<String, DataSource> next = seedBaseDataSourceHolder.getDataSourceMap().entrySet().iterator().next();
        log.info("单数据源配置启动，正在使用的数据源为{}",next.getKey());
        return next.getValue();
    }
}
